package com.springboot.wisdom.mqtt.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.springboot.wisdom.common.common.ResultUtil;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.common.common.bean.PageQueryDTO;
import com.springboot.wisdom.common.util.UserUtils;
import com.springboot.wisdom.mqtt.mapper.HardwareDeviceErrorMapper;
import com.springboot.wisdom.mqtt.service.HardwareDeviceErrorService;
import com.springboot.wisdom.serviceApi.mqtt.model.entity.HardwareDeviceErrorDO;
import com.springboot.wisdom.serviceApi.mqtt.model.vo.HardwareDeviceErrorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 10236
 * @description 针对表【hardware_device_error】的数据库操作Service实现
 * @createDate 2025-04-28 10:59:23
 */
@Service
public class HardwareDeviceErrorServiceImpl extends MPJBaseServiceImpl<HardwareDeviceErrorMapper, HardwareDeviceErrorDO>
        implements HardwareDeviceErrorService {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private HardwareDeviceErrorMapper hardwareDeviceErrorMapper;

    @Override
    public MyResult<Page<HardwareDeviceErrorVO>> getInfoPageBy(PageQueryDTO pageQueryDTO, HardwareDeviceErrorVO vo) {
        String userId = userUtils.getLoginUserId();
        System.out.println("userId = " + userId);

        // 连表求总数。s
        Long total = hardwareDeviceErrorMapper.selectJoinCount(new MPJQueryWrapper<>());

        // 连表分页查询。
        MPJLambdaWrapper<HardwareDeviceErrorDO> lambdaWrapper = new MPJLambdaWrapper<HardwareDeviceErrorDO>()
                .selectAll(HardwareDeviceErrorDO.class)
                .orderByDesc(HardwareDeviceErrorDO::getCreateTime);

        Page<HardwareDeviceErrorVO> logVOIPage = hardwareDeviceErrorMapper.selectJoinPage(
                pageQueryDTO.toPage(pageQueryDTO.getCurrent(), pageQueryDTO.getSize(), total, false)
                , HardwareDeviceErrorVO.class, lambdaWrapper);
        System.out.println(logVOIPage.getRecords());//获取分页记录
        System.out.println(logVOIPage.getPages());//总页数
        System.out.println(logVOIPage.getTotal());//总记录数
        System.out.println(logVOIPage.hasPrevious());//是否有上一页
        System.out.println(logVOIPage.hasNext());//是否有下一页
        return ResultUtil.success(logVOIPage);
    }

    @Override
    public MyResult<String> add(HardwareDeviceErrorVO vo) {
        vo.setCreateTime(null);
        vo.setUpdateTime(DateTime.now());
        boolean result = hardwareDeviceErrorMapper.insertOrUpdate(vo);
        return result ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }
}




